package com.scse.bms.domain;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;


/**
 * @Title: ResultBean
 * @Author: 吴赠禹
 * @Date: 2020/10/30 10:03
 * @Description: 封装返回数据的实体对象，包含：
 *               状态码、数据对象、描述信息。
 */

@Getter
@Setter
@ToString
public class ResultBean<T> {
    private T data;
    private int code;
    private String msg;

    /**
     * 没有数据返回，默认状态码为0，消息为：success
     */
    public ResultBean() {
        this.code = 0;
        this.msg = "success";
    }

    /**
     * 没有数据返回，可以指定状态码和消息
     * @param code
     * @param msg
     */
    public ResultBean(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 有数据返回，状态码为0，默认提示信息为：操作成功！
     * @param data
     */
    public ResultBean(T data) {
        this.data = data;
        this.code = 0;
        this.msg = "success";
    }

    /**
     * 有数据返回，状态码为0，人为指定提示信息
     * @param data
     * @param msg
     */
    public ResultBean(T data, String msg) {
        this.data = data;
        this.code = 0;
        this.msg = msg;
    }

    public ResultBean(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }


    public static ResultBean ofFail(int code) {
        ResultBean ret = new ResultBean();
        ret.setCode(code);
        return ret;
    }

    public static ResultBean ofFail(int code, String desc) {
        ResultBean ret = new ResultBean();
        ret.setCode(code);
        ret.setMsg(desc);
        return ret;
    }

}
